package morning.cat.demo1.io4;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Serializable;

import org.junit.Test;

public class InputStreamReaderTest {
	@Test
	public void test1() throws IOException {
		FileInputStream in = new FileInputStream("e:\\javaio\\imoocutf8.txt");
		// 默认项目的编码,操作的时候，要写文件本身的编码格式
		InputStreamReader isr = new InputStreamReader(in, "utf-8");

		FileOutputStream out = new FileOutputStream(
				"e:\\javaio\\imoocutf81.txt");
		OutputStreamWriter osw = new OutputStreamWriter(out, "utf-8");
		/*
		 * int c ; while((c = isr.read())!=-1){ System.out.print((char)c); }
		 */
		char[] buffer = new char[8 * 1024];
		int c;
		/*
		 * 批量读取，放入buffer这个字符数组，从第0个位置开始放置，最多放buffer.length个 返回的是读到的字符的个数
		 */
		while ((c = isr.read(buffer, 0, buffer.length)) != -1) {
			String s = new String(buffer, 0, c);
			System.out.print(s);
			osw.write(buffer, 0, c);
			osw.flush();
		}
		isr.close();
		osw.close();
	}

	@Test
	public void test2() throws IOException {
		FileReader fr = new FileReader("e:\\javaio\\imooc.txt");
		FileWriter fw = new FileWriter("e:\\javaio\\imooc2.txt");
		// FileWriter fw = new FileWriter("e:\\javaio\\imooc2.txt",true);
		char[] buffer = new char[2056];
		int c;
		while ((c = fr.read(buffer, 0, buffer.length)) != -1) {
			fw.write(buffer, 0, c);
			fw.flush();
		}
		fr.close();
		fw.close();
	}

	@Test
	public void test3() throws IOException {
		// 对文件进行读写操作
		BufferedReader br = new BufferedReader(new InputStreamReader(
				new FileInputStream("e:\\javaio\\imooc.txt")));
		/*
		 * BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( new
		 * FileOutputStream("e:\\javaio\\imooc3.txt")));
		 */
		PrintWriter pw = new PrintWriter("e:\\javaio\\imooc4.txt");
		// PrintWriter pw1 = new PrintWriter(outputStream,boolean autoFlush);
		String line;
		while ((line = br.readLine()) != null) {
			System.out.println(line);// 一次读一行，并不能识别换行
			/*
			 * bw.write(line); //单独写出换行操作 bw.newLine();//换行操作 bw.flush();
			 */
			pw.println(line);
			pw.flush();
		}
		br.close();
		// bw.close();
		pw.close();
	}

	@Test
	public void test4() throws IOException, ClassNotFoundException {
		String file = "demo/obj.dat";
		// 1.对象的序列化
		/*
		 * ObjectOutputStream oos = new ObjectOutputStream( new
		 * FileOutputStream(file)); Student stu = new Student("10001", "张三",
		 * 20); oos.writeObject(stu); oos.flush(); oos.close();
		 */
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
		Student stu = (Student) ois.readObject();
		System.out.println(stu);
		ois.close();
	}

	@Test
	public void test5() throws IOException, ClassNotFoundException {
		/*
		 * ObjectOutputStream oos = new ObjectOutputStream( new
		 * FileOutputStream("demo/obj1.dat")); Foo2 foo2 = new Foo2();
		 * oos.writeObject(foo2); oos.flush(); oos.close();
		 */

		// 反序列化是否递归调用父类的构造函数
		/*
		 * ObjectInputStream ois = new ObjectInputStream( new
		 * FileInputStream("demo/obj1.dat")); Foo2 foo2 =
		 * (Foo2)ois.readObject(); System.out.println(foo2); ois.close();
		 */

		/*
		 * ObjectOutputStream oos = new ObjectOutputStream( new
		 * FileOutputStream("demo/obj1.dat")); Bar2 bar2 = new Bar2();
		 * oos.writeObject(bar2); oos.flush(); oos.close();
		 */

		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
				"demo/obj1.dat"));
		Bar2 bar2 = (Bar2) ois.readObject();
		System.out.println(bar2);
		ois.close();

		/*
		 * 对子类对象进行反序列化操作时， 如果其父类没有实现序列化接口 那么其父类的构造函数会被调用
		 */
	}
}

/*
 * 一个类实现了序列化接口，那么其子类都可以进行序列化
 */
class Foo implements Serializable {
	public Foo() {
		System.out.println("foo...");
	}
}

class Foo1 extends Foo {
	public Foo1() {
		System.out.println("foo1...");
	}
}

class Foo2 extends Foo1 {
	public Foo2() {
		System.out.println("foo2...");
	}
}

class Bar {
	public Bar() {
		System.out.println("bar");
	}
}

class Bar1 extends Bar {
	public Bar1() {
		System.out.println("bar1..");
	}
}

class Bar2 extends Bar1 implements Serializable {
	public Bar2() {
		System.out.println("bar2...");
	}
}
